import java.awt.EventQueue;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class TPA {

	static String serverAddr = "localhost";
	static String clientAddr = "localhost";

	static int listenServerPort = 8806;
	static int sendServerPort = 8805;
	static int listenClientPort = 8802;
	static int sendClientPort = 12301;
	private JFrame frame;
	JTextArea textArea;
	
	public TPA() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame("TPA");
		frame.setBounds(100, 100, 403, 317);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblMessage = new JLabel("message");
		lblMessage.setBounds(23, 11, 55, 14);
		frame.getContentPane().add(lblMessage);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(23, 36, 319, 199);
		frame.getContentPane().add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
	}
	
	class ClientSocket extends Thread{
		public void run() {
			ServerSocket tpa;
			
			try {
				tpa = new ServerSocket(listenClientPort);
				while(true){   	
					Socket client=tpa.accept();
					DataInputStream din = new DataInputStream(client.getInputStream());
					
					String fileName = din.readUTF();
					byte[] buffer = new byte[4096];	
					int v = din.readInt();
					int w = din.readInt();
					int u = din.readInt();
					int n = din.readInt();
					
					textArea.append("receive param from client\n");
					textArea.setCaretPosition(textArea.getDocument().getLength()-1);

					MkChal mkChal = new MkChal(n);
					int[][] chal=mkChal.getChal();
					int c = mkChal.getC();
					System.out.print("c:"+c);
					
					Socket server = new Socket(serverAddr, sendServerPort);
					OutputStream os2server = server.getOutputStream();
					DataOutputStream dout2server = new DataOutputStream(os2server);
					dout2server.writeUTF(fileName);
					dout2server.writeInt(v);
					dout2server.writeInt(w);
					dout2server.writeInt(u);
					dout2server.writeInt(c);
					
					for(int i = 0; i < c; i++){
						dout2server.writeInt(chal[0][i]);
						dout2server.writeInt(chal[1][i]);
					}
					
					textArea.append("send challeng msg to server\n");
					textArea.setCaretPosition(textArea.getDocument().getLength()-1);
					dout2server.flush();
					server.shutdownOutput();
					
					client.close();
					server.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	class CloudServerSocket extends Thread{
		public void run() {
			ServerSocket tpa;
			try {
				tpa = new ServerSocket(listenServerPort);
				while(true){   	
					Socket server=tpa.accept();
					DataInputStream din = new DataInputStream(server.getInputStream());

					String fileName = din.readUTF();
					int v = din.readInt();
					int w = din.readInt();
					int u = din.readInt();
					String miu = din.readUTF();
					String sigma = din.readUTF();
					int R = din.readInt();

					int c = din.readInt();
					
					System.out.println(fileName+","+v+","+w+","+u+","+miu+","+sigma
							+","+R+","+c);
					
					int[][] chal = new int [2][c];
					for (int i = 0; i < c; i++) {
						chal[0][i] = din.readInt();
						chal[1][i] = din.readInt();
					}
					
					textArea.append("proving...\n");
					textArea.setCaretPosition(textArea.getDocument().getLength()-1);
					
					VerifyProof verifyProof = new VerifyProof(v,w,u,miu,sigma,R,chal);
					boolean result = verifyProof.verifyProof();
					
					Socket client = new Socket(clientAddr, sendClientPort);
					OutputStream os2server = client.getOutputStream();
					DataOutputStream dout2server = new DataOutputStream(os2server);
					
					dout2server.writeUTF(fileName);
					dout2server.writeBoolean(result);
					System.out.println("Boolean is:"+result);
					if (result) textArea.append("audit of file:" + fileName +" succeed\n");
					else textArea.append("audit of file:"+fileName+" fail\n");
					textArea.setCaretPosition(textArea.getDocument().getLength()-1);
					
					dout2server.flush();
					client.shutdownOutput();

					server.close();
					client.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String args[]){
	
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TPA window = new TPA();
					ClientSocket clientSocket = window.new ClientSocket();
					clientSocket.start();
					
					CloudServerSocket cloudServerSocket = window.new CloudServerSocket();
					cloudServerSocket.start();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}

